#include "Ringqueue.hpp"
#include <unistd.h>
#include <ctime>

void *comsumer(void *args)
{
    RingQueue<int> *rq = static_cast<RingQueue<int> *>(args);
    while (true)
    {
        int data = 0;
        rq->pop(&data);
        std::cout << "comsumer done " << data << std::endl;
        sleep(1);
    }
}

void *productor(void *args)
{
    RingQueue<int> *rq = static_cast<RingQueue<int> *>(args);
    while (true)
    {
        int data = rand() % 10 + 1;
        rq->push(data);
        std::cout << "product done " << data << std::endl;
    }
}

int main()
{
    srand(time(0));
    RingQueue<int> *rq = new RingQueue<int>();
    // 单生产、单消费
    //  pthread_t c, p;
    //  pthread_create(&c, nullptr, comsumer, rq);
    //  pthread_create(&p, nullptr, productor, rq);

    // pthread_join(c, nullptr);
    // pthread_join(p, nullptr);

    // 多生产多消费
    pthread_t c[3], p[2];
    for (int i = 0; i < 3; i++)
        pthread_create(c + i, nullptr, comsumer, rq);
    for (int i = 0; i < 2; i++)
        pthread_create(p + i, nullptr, productor, rq);

    for (int i = 0; i < 3; i++)
        pthread_join(c[i], nullptr);
    for (int i = 0; i < 2; i++)
        pthread_join(p[i], nullptr);
    return 0;
}
